#include "Scene.h"

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>

Scene::Scene(double fov, double ratio, double n, double f, Vector3 eye, Vector3 lookAt, Vector3 up)
{
	glClearColor(0.5f, 0.5f, 0.6f, 0.0f);
	// Z-Buffer
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(fov, ratio, n, f);
	camera = new FreeflyCamera(eye, lookAt, up);
}

Scene::~Scene() {
	for(std::list<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++)
		delete *it;
	delete camera;
}

void Scene::addObject(SceneObject* sObj)
{
	objects.push_back(sObj);
}

void Scene::removeObject(SceneObject* sObj)
{
	objects.remove(sObj);
}

void Scene::Render() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

    camera->SetView();

    for(std::list<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++)
    {
    	(*it)->Draw();
    }

    glFlush();
    SDL_GL_SwapBuffers();
}

void Scene::TreatEvent(SDL_Event evt) {
	// TODO : use flags
	if(evt.type == SDL_KEYDOWN)
		camera->OnKeyboard(evt.key);
	// Does not work, see FreeflyCamera::OnMouseMotion
	/*else if(evt.type == SDL_MOUSEMOTION)
		camera->OnMouseMotion(evt.motion);*/
}
